﻿/* Write a method that counts how many times given number appears in given array. 
* Write a test program to check if the method is working correctly.
*/




using System;

namespace CountOccurances_
{
    public class CountOccurances_
    {
        public static void Main()
        {
            int N;
            uint array_size;

            bool iProvided = false;
            do
            {
                if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter a POSITIVE integer. \r\nValid range: {0} - {1}", 1, UInt32.MaxValue);
                else iProvided = true;
                Console.Write("Please enter how many numbers you want to add: ");
            } while (UInt32.TryParse(Console.ReadLine(), out array_size) == false || array_size < 1);


            iProvided = false;         //reset iProvided to avoid getting again the error message from a previous error check
            do
            {
                if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter an integer. \r\n\tValid range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
                else iProvided = true;
                Console.Write("Enter the number of which you want to find occurances: ");
            } while (Int32.TryParse(Console.ReadLine(), out N) == false);

            int[] nums = new int[array_size];
            for (int i = 0; i < array_size; i++)
            {
                iProvided = false;         //reset iProvided to avoid getting again the error message from a previous error check
                do
                {
                    if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter an integer. \r\n\tValid range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
                    else iProvided = true;
                    Console.Write("Please enter number #{0}: ", i + 1);
                } while (Int32.TryParse(Console.ReadLine(), out nums[i]) == false);
                //Console.WriteLine(nums[i]);
            }

            int count = CountOccurances(N, nums);
            Console.WriteLine("\r\nFound {0} occurances of {1} in elements {2}\r\n", count, N, string.Join(",", nums));
        }

        public static int CountOccurances(int numbertosearch, int[] inarray)
        {
            int counter = 0;
            foreach (int var in inarray) if (numbertosearch == var) counter++;
            return counter;
        }
    }
}
